{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generating Code by building a Syntax Tree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The last, most structured alternative for generating code is to construct the tree data structure representing the syntax, and then transforming this back into source code form.\n",
    "\n",
    "This approach to code generation is perhaps the most applicable to the programmatic generation of code, and less the generation directly by a user."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from cgen import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "func = FunctionBody(\n",
    "    FunctionDeclaration(Const(Pointer(Value(\"char\", \"greet\"))), []),\n",
    "    Block([Statement('return \"hello world\"')])\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "char const *greet()\n",
      "{\n",
      "  return \"hello world\";\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 0
}